package org.speed.mes.base.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.speed.mes.base.domain.vo.BsProcessVo;
import org.speed.mes.base.domain.vo.BsWorklineVo;
import org.speed.mes.base.service.IBsProcessService;
import org.speed.mes.base.service.IBsWorklineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.speed.mes.base.domain.bo.BsWorkstationBo;
import org.speed.mes.base.domain.vo.BsWorkstationVo;
import org.speed.mes.base.domain.BsWorkstation;
import org.speed.mes.base.mapper.BsWorkstationMapper;
import org.speed.mes.base.service.IBsWorkstationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工位管理Service业务层处理
 *
 * @author speed-mes
 * @date 2024-06-15
 */
@RequiredArgsConstructor
@Service
public class BsWorkstationServiceImpl implements IBsWorkstationService {

    private final BsWorkstationMapper baseMapper;
    @Autowired
    private IBsWorklineService worklineService;
    @Autowired
    private IBsProcessService processService;

    /**
     * 查询工位管理
     */
    @Override
    public BsWorkstationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工位管理列表
     */
    @Override
    public TableDataInfo<BsWorkstationVo> queryPageList(BsWorkstationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsWorkstation> lqw = buildQueryWrapper(bo);
        Page<BsWorkstationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<BsWorkstationVo> records = result.getRecords();
        records.stream().forEach(item -> {
            BsWorklineVo workline = worklineService.queryById(item.getWorklineId());
            item.setWorklineName(workline.getName());
            BsProcessVo process = processService.queryById(item.getProcessId());
            item.setProcessName(process.getName());
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询工位管理列表
     */
    @Override
    public List<BsWorkstationVo> queryList(BsWorkstationBo bo) {
        LambdaQueryWrapper<BsWorkstation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsWorkstation> buildQueryWrapper(BsWorkstationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsWorkstation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BsWorkstation::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BsWorkstation::getName, bo.getName());
        lqw.eq(bo.getWorklineId() != null, BsWorkstation::getWorklineId, bo.getWorklineId());
        lqw.eq(bo.getProcessId() != null, BsWorkstation::getProcessId, bo.getProcessId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BsWorkstation::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增工位管理
     */
    @Override
    public Boolean insertByBo(BsWorkstationBo bo) {
        BsWorkstation add = MapstructUtils.convert(bo, BsWorkstation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工位管理
     */
    @Override
    public Boolean updateByBo(BsWorkstationBo bo) {
        BsWorkstation update = MapstructUtils.convert(bo, BsWorkstation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsWorkstation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工位管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
